import os
from enum import Enum
import platform
import platformdirs
import sqlite3


class OSType(Enum):
    WINDOWS = 'Windows'
    MACOS = 'Darwin'


class Env(Enum):
    DEV = 'dev'
    PROD = 'prod'


base_path = platformdirs.user_data_dir('TimeLog', 'YunZen')
os.makedirs(base_path, exist_ok=True)
capture_path = os.path.join(base_path, 'capture')
os.makedirs(capture_path, exist_ok=True)

db_path = os.path.join(base_path, 'timelog.db')
if not os.path.exists(db_path):
    conn = sqlite3.connect(db_path)
    cursor = conn.cursor()
    cursor.execute('''
        CREATE TABLE "main"."program_type" (
            "id" INTEGER PRIMARY KEY AUTOINCREMENT,
            "name" VARCHAR(256) NOT NULL,
            "color" VARCHAR(6) NOT NULL
        )
    ''')
    cursor.execute('''
        CREATE INDEX "idx_program_type_name" ON "program_type" ("name")
    ''')
    cursor.execute('''
        CREATE TABLE "main"."program_line" (
            "id" INTEGER PRIMARY KEY AUTOINCREMENT,
            "name" VARCHAR(256) NOT NULL,
            "title" VARCHAR(512) NOT NULL,
            "begin_time" DATETIME NOT NULL,
            "end_time" DATETIME NOT NULL
        )
    ''')
    cursor.execute('''
        CREATE TABLE "main"."tag_line" (
            "id" INTEGER PRIMARY KEY AUTOINCREMENT,
            "name" INTEGER NOT NULL,
            "begin_time" DATE NOT NULL,
            "end_time" DATE NOT NULL,
            "note" TEXT NOT NULL
        )
    ''')
    cursor.execute('''
        CREATE TABLE "main"."tag_type" (
            "id" INTEGER PRIMARY KEY AUTOINCREMENT,
            "name" VARCHAR(256) NOT NULL,
            "color" VARCHAR(6) NOT NULL
        )
    ''')
    cursor.close()


class Config:
    os_type = OSType(platform.system())
    env = Env.DEV

    base_path = base_path
    db_path = db_path
    db_uri = f'sqlite:///{db_path}?check_same_thread=False'
    capture_path = capture_path

    capture_scale = 0.33
    capture_interval = 60
    timelog_interval = 5


config = Config
